<?php

namespace App\Http\Controllers\Admin\AirLine;

use App\Http\Controllers\Controller;
use App\Models\CustomerPreferenceModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//客户排货偏好
class CustomerPreference extends Controller
{

    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $customer_name = $request->input('customer_name','');
        $route_id = $request->input('route_id', '');
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = CustomerPreferenceModel::query()
            ->with([
                'create_admin',
                'route'
            ])->whereHas('customer', function ($query) use ($customer_name) {
                $query->where('name', 'like', '%'.$customer_name.'%');
            });

        if($route_id){
            $query->where('route_id', $route_id);
        }

        if($create_start && $create_end){
            $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            return [
                'id' => $item->id,
                'customer_id' => $item->customer_id,
                'customer_name' => $item->customer? $item->customer->name : '',
                'route_id' => $item->route_id,
                'route' => $item->route ? $item->route->name : '',
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'customer_id' => 'required|integer',
            'route_id' => 'required|integer',
        ], [
            'customer_id.required' => '客户不能为空',
            'route_id.required' => '航线不能为空',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $preference = new CustomerPreferenceModel();
        $preference->customer_id = $params['customer_id'];
        $preference->route_id = $params['route_id'];
        $preference->created_admin_id = $auth->id;
        $preference->save();
        return $this->success();
    }

    public function update(Request $request, $id){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'customer_id' => 'required|integer',
            'route_id' => 'required|integer',
        ], [
            'customer_id.required' => '客户不能为空',
            'route_id.required' => '航线不能为空',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $preference = CustomerPreferenceModel::find($id);
        if(!$preference) {
            return $this->error('数据不存在');
        }
        
        $preference->customer_id = $params['customer_id'];
        $preference->route_id = $params['route_id'];
        $preference->created_admin_id = $auth->id;
        $preference->save();
        return $this->success();
    }

    public function delete(int $id){
        CustomerPreferenceModel::destroy($id);
        return $this->success();
    }

}
